package by.jabbby.service.notification;

import java.util.HashMap;
import java.util.Map;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import by.jabbby.Logger;
import by.jabbby.dataprovider.AccountProvider;
import by.jabbby.dataprovider.ContactProvider;
import by.jabbby.dataprovider.ContactProvider.ContactEntry;
import by.jabbby.service.JabberService;
import by.jabbby.ui.act.Chat;
import by.jabbby.ui.act.R;

public class NotificationBar {
	//private static int notificatonId = 1;
	public static Map<String, Integer> notificationIdHash;
	
	private int getNotificationId(Context context, String jid) throws Exception{
	/*	int result = -1;
		if(notificationIdHash == null){
			notificationIdHash = new HashMap<String, Integer>();
		}else{
			Integer tmp =  notificationIdHash.get(jid);
			if(tmp!= null){
				result = tmp;
			}
		}
		
		if(result == -1){
			ContactProvider cp  = new ContactProvider(context.getApplicationContext());
			ContactEntry ce = cp.getEntry(jid);
			if(ce == null){
				Logger.e("NotificationBar: contact whith jid does not rxist");
				throw new Exception("NotificationBar: contact whith jid does not rxist");
			}else{
				result = (int)ce.getId();
				notificationIdHash.put(jid, result);
			}
		}
		*/
	return jid.hashCode();
	//	return result;
	}
	
	//private static final int NOTIFY_ID = 1; 
	public void notifi(Context context, String jid) {
		try {
		NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); // Создаем
																																	// экземпляр
																																	// менеджера
																																	// уведомлений
		int icon = android.R.drawable.sym_action_email; 
		String tickerText = context.getString(R.string.toast_new_message);
	//	tickerText = tickerText + 
		
		long when = System.currentTimeMillis(); 
		Notification notification = new Notification(icon, tickerText, when); 
		notification.defaults |= Notification.FLAG_AUTO_CANCEL;
		
		
		// Context context = getApplicationContext();
		CharSequence contentTitle = context.getString(R.string.toast_new_message_title); 
		CharSequence contentText = context.getString(R.string.toast_new_message_content);
		contentText = contentText +jid;
		Intent notificationIntent = new Intent(context, Chat.class); 
		notificationIntent.setAction(JabberService.ACTION_NEW_MESSAGE);
		notificationIntent.putExtra("by.jabbby.ui.act.user_jid", jid);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 
		notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

		// notification.defaults = Notification.DEFAULT_SOUND;
		
			mNotificationManager.notify(getNotificationId(context, jid), notification);
		} catch (Exception e) {
			Logger.e(e);
		} 
	}
}
